fetch("medal-add.html").then(r => r.text()).then(text => {
	let medaladdDialog = $(text);
	$("body").append(medaladdDialog);

	let form = medaladdDialog[0].querySelector("form");//form是原生标签元素，非jquery对象
	let btn = medaladdDialog[0].querySelector("._save");
	let seId = form.seId;
	let sportsmanId = form.sportsmanId;
	
	fetch("SportingEvents-list.let").then(r=>r.json()).then(data=>{
		data.forEach(item=>{
			seId.add(new Option(item.seName,item.seId));
		})
	});
	fetch("Sportsman-list.let").then(r=>r.json()).then(data=>{
		data.forEach(item=>{
			sportsmanId.add(new Option(item.sportsmanName,item.sportsmanId));
		})
	});

	console.debug(medaladdDialog, form, btn);

	btn.onclick = function() {
		//验证输入参数
		let param = new URLSearchParams(new FormData(form));
		console.info(param.toString());
		fetch("medal-add.let", {
			method: 'POST',
			mode: 'cors',
			cache: 'no-cache',
			credentials: 'include',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			redirect: 'follow',
			referrerPolicy: 'no-referrer',
			body: param
		}).then(r => r.json()).then(data => {
			if (data.ok) {
				medaladdDialog.modal('hide');
				form.reset();
				window.postMessage("loadData");//发送消息
			} else {
				alert(data.msg);
			}
			console.debug(data);
		}).catch(e => {
			console.error(e);
		});
	}
});

export default function() {
	//medaladdDialog.modal('show');
}